package cn.lyjuan.archetypes.bms.web.service;


import cn.lyjuan.archetypes.bms.common.entity.MgManage;
import cn.lyjuan.archetypes.bms.web.bean.dto.GroupActionBean;
import cn.lyjuan.archetypes.bms.web.mapper.IWebActionGroupMapper;
import cn.lyjuan.archetypes.bms.web.mapper.IWebActionMapper;
import cn.lyjuan.archetypes.bms.web.util.ErrUtils;
import cn.lyjuan.archetypes.bms.web.util.SuperAdminUtils;
import com.github.pagehelper.PageHelper;
import cn.lyjuan.archetypes.bms.common.entity.MgAction;
import cn.lyjuan.archetypes.bms.common.mapper.MgActionGroupMapper;
import cn.lyjuan.archetypes.bms.common.mapper.MgActionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/11.
 */
@Service("mgActionService")
public class ActionService
{
    @Autowired(required = false)
    private MgActionGroupMapper actionGroupMapper;

    @Autowired(required = false)
    private MgActionMapper actionMapper;

    @Autowired
    private IWebActionGroupMapper webActionGroupMapper;

    @Autowired
    private IWebActionMapper webActionMapper;

    public List<String> queryAllActions()
    {
        return webActionMapper.selectAll();
    }

    public List<String> queryAllActions(Integer manageId)
    {
        return webActionMapper.selectAllByManagerId(manageId);
    }

    /**
     * 查询登陆用户可用权限
     *
     * @param manage  当前登陆用户编号
     * @param onlyNav 仅显示导航请求
     * @return
     */
    public List<GroupActionBean> queryActionsForManage(MgManage manage, boolean onlyNav)
    {
        return webActionGroupMapper.selectActionsForManage(manage.getId(), onlyNav);
    }

    /**
     * 查询管理员有权限的请求
     * @param manage
     * @param param
     * @return
     */
    public List<MgAction> queryActionsForManage(MgManage manage, Map<String, String> param)
    {
        List<MgAction> list = null;
        if (SuperAdminUtils.isAdmin(manage.getName()))
            list = webActionMapper.selectActions(param);
        else
            list = webActionMapper.selectActionsForManage(manage.getId(), param);

        return list;
    }

    /**
     * 分页检索查询请求
     *
     * @param param
     * @param pn
     * @param ps
     * @return
     */
    public List<MgAction> queryActionsForManage(MgManage manage, Map<String, String> param, Integer pn, Integer ps)
    {
        PageHelper.startPage(pn, ps);

        return queryActionsForManage(manage, param);
    }

    /**
     * 角色已有的请求
     *
     * @param roleid
     * @return
     */
    public List<MgAction> queryActionsForRole(int roleid)
    {
        return webActionMapper.selectActionsForRole(roleid);
    }

    /**
     * 添加请求
     *
     * @param action
     */
    public void addAction(MgAction action)
    {
        // 请求地址是否重复
        MgAction actionByUrl = webActionMapper.selectByUrl(action.getActionurl());

        if (null != actionByUrl)
            ErrUtils.appThrow("请求地址已存在");

        actionMapper.insert(action);
    }

    /**
     * 根据编号查询请求信息
     *
     * @param actionid
     * @return
     */
    public MgAction queryActionById(Integer actionid)
    {
        return actionMapper.selectByPrimaryKey(actionid);
    }

    /**
     * 更新请求
     *
     * @param action
     */
    public void updateAction(MgAction action)
    {
        actionMapper.updateByPrimaryKeySelective(action);
    }

    /**
     * 锁定请求
     *
     * @param id
     */
    public void updateStatus(Integer id, Integer status)
    {
        webActionMapper.updateStatusById(id, status);
    }
}
